package MovieFinderApp.MCColonDelimitedMovieFinder;

import java.util.*;
import java.io.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import MovieFinderApp.MCMovieFinderInterfaces.*;
import MovieFinderApp.LCMovieInterfaces.IMovie;

public class ColonDelimitedMovieFinder implements IMovieFinder, ICustomizeMovieFinder {
   private String filename;
   
   public ColonDelimitedMovieFinder(){

   }
   
   public void setFilename(String filename){
      this.filename = filename;
   }
   
   public ArrayList<IMovie> findAll(){
      ArrayList<IMovie> movies = new ArrayList<IMovie>();
      try{
         BufferedReader in = new BufferedReader(
                             new InputStreamReader(
                             new FileInputStream(filename)));
         
         try{
            String line;
            while((line = in.readLine()) != null){
               int colonPos = line.indexOf(":");
               ApplicationContext appCon = new FileSystemXmlApplicationContext("classpath:MovieFinderApp/MCColonDelimitedMovieFinder/properties/config.xml");
               IMovie mov = (IMovie)appCon.getBean("myMovie");
               mov.setDirector(line.substring(colonPos+1));
               mov.setTitle(line.substring(0, colonPos));
               movies.add(mov);
            }
         }
         finally{
            in.close();
         }
      }
      catch(IOException ioe){
         System.out.println(ioe);
      }
      catch(Exception ex){
         System.out.println(ex);
      }
      
      return movies;
   }
}
